package com.hengxinguotong.zhihuichengjian.notification;

import android.app.Notification;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/14.
 * 读取系统通知栏颜色工具类
 * Created by liuboyu on 16/12/21.
 */
public class SystemColorUtils {

    private static final String DUMMY_TITLE = "DUMMY_TITLE";
    private static final double COLOR_THRESHOLD = 180.0;
    private static int titleColor;

    /**
     * 获取通知栏颜色
     * @param context
     * @return
     */
    public static int getNotificationColor(Context context) {
//        if (context instanceof AppCompatActivity) {
//            return getNotificationColorCompat(context);
//        } else {
        return getNotificationColorInternal(context);
//        }
    }

    /**
     * 当前状态了是否为暗色
     *
     * @param context
     * @return
     */
    public static boolean isDarkNotificationBar(Context context) {
        return !isColorSimilar(Color.BLACK, getNotificationColor(context));
    }


    /**
     * notificationRoot了，不如就遍历它，先找到其中的所有TextView
     * 取字体最大的TextView作为title（这是合理的，
     * 因为默认通知中最多也就4个TextView，分别是title、
     * content、info、when，title肯定是字体最大，最显眼的）
     *
     * @param context
     * @return
     */
    public static int getNotificationColorCompat(Context context) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.build();
        int layoutId = notification.contentView.getLayoutId();
        ViewGroup notificationRoot = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null);
        final TextView title = (TextView) notificationRoot.findViewById(android.R.id.title);
        //ROM厂商会把id改掉，导致找到的title为空。
        if (null == title) {
            final List<TextView> textViews = new ArrayList<>();
            iteratorView(notificationRoot, new Filter() {
                @Override
                public void filter(View view) {
                    if (view instanceof TextView) {
                        textViews.add((TextView) view);
                    }
                }
            });

            float minTextSize = Integer.MIN_VALUE;
            int index = 0;
            for (int i = 0; i < textViews.size(); i++) {
                float currentSize = textViews.get(i).getTextSize();
                if (currentSize > minTextSize) {
                    minTextSize = currentSize;
                    index = i;
                }
            }
            return textViews.get(index).getCurrentTextColor();
        } else {
            return title.getCurrentTextColor();
        }
    }

    /**
     * 5.0以下的机器
     *
     * @param context
     * @return
     */
    public static int getNotificationColorInternal(Context context) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.build();
        int layoutId = notification.contentView.getLayoutId();
        ViewGroup notificationRoot = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null);
        final TextView title = (TextView) notificationRoot.findViewById(android.R.id.title);
        if (null == title) {
            iteratorView(notificationRoot, new Filter() {

                @Override
                public void filter(View view) {
                    if (view instanceof TextView) {
                        TextView textView = (TextView) view;
                        if (DUMMY_TITLE.equals(textView.getText().toString())) {
                            titleColor = textView.getCurrentTextColor();
                        }
                    }
                }
            });
            return titleColor;
        } else {
            Log.e("ddddd3ddd",""+title.getCurrentTextColor());
            return title.getCurrentTextColor();
        }
    }

    /**
     * 遍历 notificationRoot了
     *
     * @param view
     * @param filter
     */
    private static void iteratorView(View view, Filter filter) {
        if (view == null || filter == null) {
            return;
        }
        filter.filter(view);
        if (view instanceof ViewGroup) {
            ViewGroup container = (ViewGroup) view;
            for (int i = 0, j = container.getChildCount(); i < j; i++) {
                View childAt = container.getChildAt(i);
                iteratorView(childAt, filter);
            }
        }
    }

    private interface Filter {
        void filter(View view);
    }

    /**
     * 使用方差来计算这个颜色是否近似黑色
     *
     * @param baseColor
     * @param color
     * @return
     */
    public static boolean isColorSimilar(int baseColor, int color) {
        int simpleBaseColor = baseColor | 0xff000000;
        int simpleColor = color | 0xff000000;
        int baseRed = Color.red(simpleBaseColor) - Color.red(simpleColor);
        int baseGreen = Color.green(simpleBaseColor) - Color.green(simpleColor);
        int baseBlue = Color.blue(simpleBaseColor) - Color.blue(simpleColor);
        double value = Math.sqrt(baseRed * baseRed + baseGreen * baseGreen + baseBlue * baseBlue);
        if (value < COLOR_THRESHOLD) {
            return true;
        }
        return false;
    }

}
